import { type CrudConfig, type FormFieldConfig } from '@/components';
import DICT_CONSTANTS from '@/constants/dictConstants';
import urlConstants from '@/constants/urlConstants';
import { getRequestData } from '@/utils';
import type { ProColumns } from '@ant-design/pro-components';
import { Tag } from 'antd';

// 审核数据类型
export interface AduitRecord {
  paramIndex: number;
  aduitPage: string;
  aduitUser: string;
  aduitStatus: string;
  aduitTime: string;
  applyUser: string;
  applyTime: string;
  aduitComment?: string;
  applyContent?: string;
}

// 统一的页面配置对象
export const pageConfig = {
  // CRUD配置
  crudConfig: {
    endpoints: {
      list: urlConstants.ADUIT_MANAGE.LIST,
      create: urlConstants.ADUIT_MANAGE.CREATE,
      update: urlConstants.ADUIT_MANAGE.EDIT,
      delete: urlConstants.ADUIT_MANAGE.DELETE,
      detail: urlConstants.ADUIT_MANAGE.LIST,
    },
    callbacks: {
      beforeCreate: async (data: any) => getRequestData(data, 'create', null),
      beforeUpdate: async (data: any) => getRequestData(data, 'edit', null),
      afterCreate: async (response: any) => console.log('审核记录创建成功:', response),
      afterUpdate: async (response: any) => console.log('审核记录更新成功:', response),
      afterDelete: async (response: any, id: string) => console.log('审核记录删除成功:', id),
      onError: (error: any, operation: string) => console.error(`审核操作 ${operation} 失败:`, error),
    },
  } as CrudConfig,

  // 搜索字段配置
  searchFields: [
    {
      name: 'aduitPage',
      label: '审核页面',
      type: 'input' as const,
      placeholder: '请输入审核页面',
    },
    {
      name: 'aduitStatus',
      label: '审核状态',
      type: 'select' as const,
      options: Object.entries(DICT_CONSTANTS.PARAM_STATUS || {}).map(([key, value]) => ({
        label: value as string,
        value: key,
      })),
    },
  ],

  // 表格列配置
  columns: [
    {
      title: '序号',
      dataIndex: 'paramIndex',
      key: 'paramIndex',
      width: 80,
      fixed: 'left',
    },
    {
      title: '审核页面',
      dataIndex: 'aduitPage',
      key: 'aduitPage',
      width: 150,
      ellipsis: true,
      render: (text: string) => <span style={{ fontWeight: 500, color: '#1890ff' }}>{text}</span>,
    },
    {
      title: '审核人',
      dataIndex: 'aduitUser',
      key: 'aduitUser',
      width: 120,
      render: (text: string) => {
        const labelTypeMap = DICT_CONSTANTS.LABEL_TYPE || {};
        return <Tag color="blue">{labelTypeMap[text] || text}</Tag>;
      },
    },
    {
      title: '审核状态',
      dataIndex: 'aduitStatus',
      key: 'aduitStatus',
      width: 100,
      render: (text: string) => {
        const paramStatusMap = DICT_CONSTANTS.PARAM_STATUS || {};
        const statusMap = {
          approved: { color: 'green', text: '已通过' },
          rejected: { color: 'red', text: '已拒绝' },
          pending: { color: 'orange', text: '待审核' },
        };
        const status = statusMap[text as keyof typeof statusMap] || {
          color: 'default',
          text: paramStatusMap[text] || text,
        };
        return <Tag color={status.color}>{status.text}</Tag>;
      },
    },
    {
      title: '审核时间',
      dataIndex: 'aduitTime',
      key: 'aduitTime',
      width: 160,
      valueType: 'dateTime',
      sorter: true,
    },
    {
      title: '申请人',
      dataIndex: 'applyUser',
      key: 'applyUser',
      width: 120,
      ellipsis: true,
    },
    {
      title: '申请时间',
      dataIndex: 'applyTime',
      key: 'applyTime',
      width: 160,
      valueType: 'dateTime',
      sorter: true,
    },
    {
      title: '审核意见',
      dataIndex: 'aduitComment',
      key: 'aduitComment',
      width: 200,
      ellipsis: true,
      render: (text: string) => text || <span style={{ color: '#999' }}>无</span>,
    },
  ] as ProColumns<AduitRecord>[],

  // 表单字段配置
  formFields: [
    {
      name: 'aduitPage',
      label: '审核页面',
      type: 'input' as const,
      placeholder: '请输入审核页面',
      rules: [{ required: true, message: '请输入审核页面' }],
    },
    {
      name: 'aduitUser',
      label: '审核人',
      type: 'select' as const,
      options: Object.entries(DICT_CONSTANTS.LABEL_TYPE || {}).map(([key, value]) => ({
        label: value as string,
        value: key,
      })),
      rules: [{ required: true, message: '请选择审核人' }],
    },
    {
      name: 'aduitStatus',
      label: '审核状态',
      type: 'select' as const,
      options: Object.entries(DICT_CONSTANTS.PARAM_STATUS || {}).map(([key, value]) => ({
        label: value as string,
        value: key,
      })),
      rules: [{ required: true, message: '请选择审核状态' }],
    },
    {
      name: 'aduitTime',
      label: '审核时间',
      type: 'date' as const,
      rules: [{ required: true, message: '请选择审核时间' }],
    },
    {
      name: 'applyUser',
      label: '申请人',
      type: 'input' as const,
      placeholder: '请输入申请人',
      rules: [{ required: true, message: '请输入申请人' }],
    },
    {
      name: 'applyTime',
      label: '申请时间',
      type: 'date' as const,
      rules: [{ required: true, message: '请选择申请时间' }],
    },
    {
      name: 'aduitComment',
      label: '审核意见',
      type: 'textarea' as const,
      placeholder: '请输入审核意见',
      span: 24,
    },
    {
      name: 'applyContent',
      label: '申请内容',
      type: 'textarea' as const,
      placeholder: '请输入申请内容',
      span: 24,
    },
  ] as FormFieldConfig[],

  // 字典枚举配置
  dictEnum: {
    // 可以在这里添加字典枚举配置
  },
};
